import os
import logging
import sys

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.api import users

class UserHomePage(webapp.RequestHandler):
	def get(self):
		user = users.GetCurrentUser()
		if user:
			# User is already logged in. 

			nickname = user.nickname()

			self.response.headers['Content-Type'] = "text/html"
			self.response.out.write('''
				<html>
					<head> 
						<title> 
								DVD Share - Welcome %s 
						</title> 
						<script 
							src="http://ajax.googleapis.com/ajax/libs/dojo/1.6.0/dojo/dojo.xd.js" 
							type="text/javascript" 
							djConfig="parseOnLoad: true">
						</script>
						<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dijit/themes/claro/claro.css"
						 />
						<style type="text/css">
						.newDvdIcon { 
						   background-image:  url('/images/add-dvd.jpg');
						   background-repeat: no-repeat; 
						   background-position: top left; 
						   width: 20px; 
						   height: 16px; 
						}
						</style>
						<script type="text/javascript">
							dojo.require("dijit.layout.TabContainer");
							dojo.require("dijit.layout.ContentPane");
							dojo.require("dijit.form.Button");
							dojo.require("dijit.form.TextBox");
							dojo.require("dijit.form.NumberTextBox");							
						</script>
						<script type="text/javascript" src="/script/common.js"> </script>
						<script type="text/javascript">
						
						var searchInput, searchButton, dvdNameInput, dvdActorsInput, dvdDirectorsInput, dvdMusicDirectorsInput, dvdYearInput, dvdSubmitButton, dvdResetButton;
						var currYear = (new Date).getFullYear();
						
						var homeSearchButtonCB = function() {
							// This is to respond to the Search button click. For now, just briefly update the status line 
							statusLine.setStatusMessage("Search bar is not enabled yet! Come back later and try again..");
						}
						
						var addNewDvdCB = function() {
							// This is a placeholder for now
							// TODO
						}

						var resetNewDvdCB = function() {
							// This is a placeholder for now
							// TODO
							dvdNameInput.attr("value", "");
							dvdActorsInput.attr("value", "");
							dvdDirectorsInput.attr("value", "");
							dvdMusicDirectorsInput.attr("value", "");
							dvdYearInput.attr("value", currYear);							
						}
						
						dojo.addOnLoad(function() {
						    // The top level search area creation 
						     searchInput = new dijit.form.TextBox( {
																   name:"homeSearchInput",
																   value:"",
																   placeHolder:"Enter movie name, actor(s) or any movie related keyword"
																}, 
																"homeSearchInput");
																
							 searchButton = new dijit.form.Button({
						    										label: "Search for DVDs",
																	onClick: homeSearchButtonCB,
						                					   },
						                					   "homeSearchButton");

						    // The new DVD addition related dijits
							dvdNameInput = new dijit.form.TextBox( {
																name: "newDvdNameInput",
																value:"",
																placeHolder:"Enter movie name"
															},
															"newDvdNameInput");
							dvdActorsInput = new dijit.form.TextBox( {
																name: "newDvdActorsInput",
																value:"",
																placeHolder:"Enter comma seperated actor(s)"
															},
															"newDvdActorsInput");
							dvdDirectorsInput = new dijit.form.TextBox( {
																name: "newDvdDirectorsInput",
																value:"",
																placeHolder:"Enter comma seperated director(s)"
															},
															"newDvdDirectorsInput");
							dvdMusicDirectorsInput = new dijit.form.TextBox( {
																name: "newDvdMusicDirectorsInput",
																value:"",
																placeHolder:"Enter comma seperated music director(s)"
															},
															"newDvdMusicDirectorsInput");															
							dvdYearInput = new dijit.form.NumberTextBox( {
																name: "newDvdYearInput",
																pattern:"####",
																value:currYear,
																placeHolder:"Enter release year in YYYY format"
															},
															"newDvdYearInput");															
							dvdSubmitButton = new dijit.form.Button( {
																label: "Add DVD details",
																onClick: addNewDvdCB,
															},
															"newDvdSubmitButton");
							dvdResetButton = new dijit.form.Button( {
																label: "Reset DVD details",
																onClick: resetNewDvdCB,
															},
															"newDvdResetButton");															
						});
						</script>
					</head>
					<body class="claro">
						<h4> <a href="%s">Logout</a> </h4>
						<h3> Welcome %s, to DVD Share </h3>
						<input id="homeSearchInput"> </input> 
						<button id="homeSearchButton"> </button> 						
						
						<p>						
						<div dojoType="dijit.layout.TabContainer" id="homeTabOwnContainer" style="width:80%%; height:250px;"
							 tabStrip="true">
							<div dojoType="dijit.layout.ContentPane" id="homeTabOwnList" title="My DVD(s)" > 
								<table id="homeTabOwnTable"> </table>
								This is to display all DVDs that I own and have taken/given from/to my friends
							</div>
							<div dojoType="dijit.layout.ContentPane" id="homeTabOwnAdd" title=" + " tooltip="Click here to add new DVDs"> 
								<table>
								<tr> 
								  <td> Movie Name: </td>
								  <td> <input id="newDvdNameInput"> </input> </td>
								</tr>
								<tr>
								  <td> Actor(s): </td>
								  <td> <input id="newDvdActorsInput"> </input> </td>
								</tr>
								<tr>
								  <td> Director(s): </td>
								  <td> <input id="newDvdDirectorsInput"> </input> </td>
								</tr>
								<tr>
								  <td> Music Director(s): </td>
								  <td> <input id="newDvdMusicDirectorsInput"> </input> </td>
								</tr>
								<tr>
								  <td> Year of release: </td>
								  <td> <input id="newDvdYearInput"> </input> </td>
								</tr>								
								</table>
								<button id="newDvdSubmitButton"> </button> <button id="newDvdResetButton"> </button>
							</div>							
						</div>
						</p>
						<p>
						<div dojoType="dijit.layout.TabContainer" style="width:80%%; height:250px;"
							 tabStrip="true">	
							<div dojoType="dijit.layout.ContentPane" id="homeTabGiven" title="My friends DVD(s)">
								<table id="homeTabGivenTable"> </table>
								This is to display all DVDs my friends have
							</div>
						</div>	
						</p>
						<p>
						<div dojoType="dijit.layout.TabContainer" style="width:80%%; height:250px;"
								 tabStrip="true">								
							<div dojoType="dijit.layout.ContentPane" id="homeTabTaken" title="My wishlist DVD(s)">
								<table id="homeTabTakenTable"> </table>
								This is to display all DVDs that I wish to have 
							</div>
						</div>
						</p>
					</body>
				</html>
				''' % (nickname, users.create_logout_url(self.request.path), nickname))
					
		else:
			# User is not logged in! Redirect to the login page
			self.redirect("/kumar/")
						
	
application = webapp.WSGIApplication([('/kumar/home', UserHomePage)], debug=True)
				 
def main():
	# main entry point

	util.run_wsgi_app(application);	
	

if __name__ == "__main__":
	main()
	
